<?php
require APPPATH.'libraries/ApiController.php';
class user extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	public function list_by_page(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["query",false,"姓名或手机号",''],
			["curpage",true,"当前页码",1],
			["pagesize",false,"每面数",20],
			],'post');
			
			$curpage=(int)$data["curpage"];
			$pagesize=(int)$data["pagesize"];
			$offset=($curpage-1)*$pagesize;
			//获取总页数
			$this->db->where("shopid",$token["shopid"]);
			$this->db->where("shopid=".$token["shopid"]." and (username like '%".$data["query"]."%' or mobile like '%".$data["query"]."%')");
			$numrows=$this->db->select("count(*) as count")->get("user")->row_array()["count"];
			
			
			$this->db->where("shopid",$token["shopid"]);
			$this->db->where("shopid=".$token["shopid"]." and (username like '%".$data["query"]."%' or mobile like '%".$data["query"]."%')");
			$this->db->limit($pagesize,$offset);
			$users=$this->db->get("user")->result_array();
			
			for($i=0;$i<count($users);$i++){
				$users[$i]["avatar"]=$users[$i]["avatar"]==""? "":$this->config->item("domain").$users[$i]["avatar"];
			}

			$this->WRITE("code",1);
			$this->WRITE("pagenum",ceil($numrows/$pagesize));
			$this->WRITE("numrows",(int)$numrows);
			$this->WRITE("list",$users);
			$this->RESPONSE();
		}	
	}
	
	public function query(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["mobile",false,"姓名或手机号",'']
			],'post');
			$this->db->where("shopid=".$token["shopid"]);
			$this->db->where("mobile",$data["mobile"]);
			$this->db->select("uid,username,ucode,mobile,money,times,score,concat('".($this->config->item("domain"))."',avatar) as avatar");
			$user=$this->db->get("user")->row_array();
			if($user){
				$this->WRITE("code",1);
				$this->WRITE("msg","查询会员成功!");
				$this->WRITE("user",$user);
				$this->RESPONSE();
			}else{
				$this->WRITE("code",0);
				$this->WRITE("msg","未查到会员信息!");
				$this->RESPONSE();
			}
			
		}	
	}
	public function query_by_uid(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"]
			],'post');
			$this->db->where("shopid=".$token["shopid"]);
			$this->db->where("uid",$data["uid"]);
			$this->db->select("uid,username,ucode,mobile,money,times,score,concat('".($this->config->item("domain"))."',avatar) as avatar");
			$user=$this->db->get("user")->row_array();
			if($user){
				$this->WRITE("code",1);
				$this->WRITE("msg","查询会员成功!");
				$this->WRITE("user",$user);
				$this->RESPONSE();
			}else{
				$this->WRITE("code",0);
				$this->WRITE("msg","未查到会员信息!");
				$this->RESPONSE();
			}
			
		}	
	}
	
	public function create(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["username",true,"会员姓名"],
			["mobile",true,"会员手机号"],
			["gender",true,"会员性别"],
			["ucode",true,"会员卡号"],
			["avatar",false,"头像",""],
			["birth",false,"会员生日",""],
			["idcard",false,"身份证号",""],
			["email",false,"电子邮件",""],
			["address",false,"会员地址",""],
			["memo",false,"备注信息",""]],'post');
			

			
			//是查询手机号是否被注册过(不同店铺可以重复使用)
			$this->db->where("mobile",$data["mobile"]);
			$this->db->where("shopid",$token["shopid"]);
			$count=$this->db->select("count(*) as count")->get("user")->row_array()["count"];
			if($count>0){
				$this->WRITE("code",0);
				$this->WRITE("msg"," 此手机号已被占用!");
				$this->RESPONSE();	
				exit();
			}
			
			//会员卡认证
			$this->db->where("ucode",$data["ucode"]);
			$this->db->where("shopid",$token["shopid"]);
			$count=$this->db->select("count(*) as count")->get("user")->row_array()["count"];
			if($count>0){
				$this->WRITE("code",0);
				$this->WRITE("msg"," 此会员卡号已被使用!");
				$this->RESPONSE();	
				exit();
			}
			
			$this->db->where("shopid",$token["shopid"]);
			$data["shopid"]=$token["shopid"];
			
			//上传头像		
			if (!file_exists($this->config->item("upload")."/uploads/avatar/")) {
			    mkdir($this->config->item("upload")."/uploads/avatar/", 0777, true);
			}
			
			foreach ($_FILES as $key => $file) {
				if (strpos($file["type"], 'image') !== false) {
					$ext = substr(strrchr($file["type"], "/"), 1);
					$filename = 'avatar_'.$data["ucode"].'.'.$ext;
					$path='/uploads/avatar/'.$filename;
					if (move_uploaded_file($file["tmp_name"],$this->config->item("upload").$path)) {
						//上传成功
						$data["avatar"]=$path;
					}
				}
			}
			$res=$this->db->insert("user",$data);
			$this->WRITE("code",1);
			$this->WRITE("msg","会员登记成功!");
			$this->RESPONSE();
		}
	}
	
	public function detail(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([["uid",true,"用户ID"]],'post');
			
			$this->db->where("uid",$data["uid"]);
			$user=$this->db->get("user")->row_array();
			$user["avatar"]=$user["avatar"]==""? "":$this->config->item("domain").$user["avatar"];
			
			//获取菜单
			$this->db->where_in("pageid",[21,1,2,7,8,18]);
			$this->db->select("pageid,title,url,concat('".($this->config->item("static"))."',icon) as icon");
			$this->db->order_by("order",'desc');
			$modules=$this->db->get("page")->result_array();
			
			$this->WRITE("code",1);
			$this->WRITE("modules",$modules);
			$this->WRITE("detail",$user);
			$this->RESPONSE();
		}
	}
	
	public function quickpay(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([["uid",true,"用户ID"]],'post');
 
		}
	}

	/*更换卡号*/
	public function changeucode(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"用户ID"],
			["newucode",true,"新会员卡号"]],"post");
			
			
			$data["ucode"]=$data["newucode"];
			unset($data["newucode"]);
			$this->db->where("uid",$data["uid"]);
			$this->db->update("user",$data);
			
			
			$this->WRITE("code",1);
			$this->WRITE("msg","更换会员卡成功!");
			$this->RESPONSE();
		}
	}
	
	public function update(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"用户ID"],
			["username",true,"会员姓名"],
			["mobile",true,"会员手机号"],
			["gender",true,"会员性别"],
			["ucode",true,"会员卡号"],
			["avatar",false,"头像",""],
			["birth",false,"会员生日",""],
			["idcard",false,"身份证号",""],
			["email",false,"电子邮件",""],
			["address",false,"会员地址",""],
			["memo",false,"备注信息",""]],'post');
			
			
			//是查询手机号是否被注册过(不同店铺可以重复使用)
			$this->db->where("mobile",$data["mobile"]);
			$this->db->where("shopid",$token["shopid"]);
			$user=$this->db->get("user")->row_array();
			if($user){
				if($user["uid"]!=$data["uid"]){
					$this->WRITE("code",0);
					$this->WRITE("msg","此手机号已被占用!");
					$this->RESPONSE();	
					exit();
				}
			}
			
			//如果有上传的文件
			if($_FILES){
				//删除头像
				$oldavatar=$this->db->select("avatar")->where("uid",$data["uid"])->get("user")->row_array()["avatar"];
				if($oldavatar!=""){
					if(file_exists($this->config->item("upload").$oldavatar)){
						unlink($this->config->item("upload").$oldavatar);
					}
				}
				
				//保存上传头像
				foreach ($_FILES as $key => $file) {
					if (strpos($file["type"], 'image') !== false  ) {
						$ext = pathinfo($file["name"],PATHINFO_EXTENSION);
						$filename = 'avatar_'.uniqid().'.'.$ext;
						$path='/uploads/avatar/'.$filename;
						if (move_uploaded_file($file["tmp_name"],$this->config->item("upload").$path)) {
							//上传成功
							$data["avatar"]=$path;
						}
					}
				}
				
				$this->db->where("uid",$data["uid"]);
				$res=$this->db->update("user",$data);
			}

			$user=$this->db->where("uid",$data["uid"])->get("user")->row_array();
			$user["avatar"]=$user["avatar"]==""? "":$this->config->item("domain").$user["avatar"];
			$this->WRITE("userinfo",$user);
			$this->WRITE("code",1);
			$this->WRITE("msg","更新成功!");
			$this->RESPONSE();
		}
	}
}
